include_guard()

if(NOT DEFINED TARGET_PLATFORM)
    # Xbox detection assumes VS generator.
    if(CMAKE_GENERATOR_PLATFORM MATCHES "^Gaming\.Xbox\.(.*)\.x64$")
        set(TARGET_XBOX TRUE CACHE INTERNAL "")
        set(TARGET_XBOX_TYPE ${CMAKE_MATCH_1} CACHE INTERNAL "")
        if(CMAKE_MATCH_1 STREQUAL Scarlett)
            set(TARGET_XBOX_SCARLETT TRUE CACHE INTERNAL "")
            set(TARGET_PLATFORM XBOX_SCARLETT CACHE INTERNAL "")
            set(TARGET_XBOX_FILE_SUFFIX xs CACHE INTERNAL "")
        elseif(CMAKE_MATCH_1 STREQUAL XboxOne)
            set(TARGET_XBOX_XBOXONE TRUE CACHE INTERNAL "")
            set(TARGET_PLATFORM XBOX_XBOXONE CACHE INTERNAL "")
            set(TARGET_XBOX_FILE_SUFFIX x CACHE INTERNAL "")
        endif()
    elseif(NOT WIN32)
        set(TARGET_WSL TRUE CACHE INTERNAL "")
        set(TARGET_PLATFORM WSL CACHE INTERNAL "")
    else()
        set(TARGET_WINDOWS TRUE CACHE INTERNAL "")
        set(TARGET_PLATFORM WINDOWS CACHE INTERNAL "")
    endif()

    message(STATUS "Detecting target platform: ${TARGET_PLATFORM}")
endif()

if(NOT DEFINED TARGET_ARCH)
    if(WIN32)
        set(arch "${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
    else()
        set(arch "${CMAKE_SYSTEM_PROCESSOR}")
    endif()

    if("${arch}" STREQUAL X86)
        set(TARGET_ARCH X86 CACHE INTERNAL "")
    elseif("${arch}" STREQUAL x64)
        set(TARGET_ARCH X64 CACHE INTERNAL "")
    elseif("${arch}" STREQUAL x86_64)
        set(TARGET_ARCH X64 CACHE INTERNAL "")
    elseif("${arch}" STREQUAL ARMV7)
        set(TARGET_ARCH ARM CACHE INTERNAL "")
    elseif("${arch}" STREQUAL ARM64)
        set(TARGET_ARCH ARM64 CACHE INTERNAL "")
    else()
        message(FATAL_ERROR "Cannot determine target architecture!")
    endif()

    unset(arch)
    message(STATUS "Detecting target architecture: ${TARGET_ARCH}")
endif()